﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using E2_WEBSITE.Code;
using E2_WEBSITE.Code.Admin;

namespace E2_WEBSITE.cp.admin.user
{
    public partial class updateinfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (Session["Authentication"] == null || Convert.ToInt32(Session["Authentication"]) != 0)
            {
                WebMsgBox.Show("Bạn phải là admin để có thể sử dụng chức năng này.");
                return;
            }
            try
            {
                int uid = int.Parse(Session["UserID"].ToString());
                UserController controller = new UserController();

                UserInfo user = controller.GetUserInfo(uid);
                user.Name = (detailUser.Rows[0].FindControl("tbName") as TextBox).Text;
                user.Email = (detailUser.Rows[0].FindControl("tbEmail") as TextBox).Text;
                user.Address = (detailUser.Rows[0].FindControl("tbAddress") as TextBox).Text;
                user.Gender = bool.Parse((detailUser.Rows[0].FindControl("rblGender") as RadioButtonList).SelectedValue);
                user.BirthDay = (detailUser.Rows[0].FindControl("dtpBirth") as BasicFrame.WebControls.BDPLite).SelectedDate;
                user.Occupation = (detailUser.Rows[0].FindControl("tbOccupation") as TextBox).Text;
                user.Phone = (detailUser.Rows[0].FindControl("tbPhone") as TextBox).Text;
                // lay thong tin
                int score = 0;
                if (!(detailUser.Rows[0].FindControl("tbScore") as TextBox).Text.Equals(string.Empty))
                {
                    score = int.Parse((detailUser.Rows[0].FindControl("tbScore") as TextBox).Text);
                }
                user.Score = score;
                user.Type = int.Parse((detailUser.Rows[0].FindControl("ddlUserType") as DropDownList).SelectedValue);
                if (controller.UpdateUser(uid, user))
                {
                    WebMsgBox.Show("Cập nhật người dùng thành công.");
                }
            }
            catch (Exception)
            {
                Response.Redirect(Utilities.ErrorPagePath);
                throw;
            }
        }

        protected void btnResetPassword_Click(object sender, EventArgs e)
        {
            if (Session["Authentication"] == null || Convert.ToInt32(Session["Authentication"]) != 0)
            {
                WebMsgBox.Show("Bạn phải là admin để có thể sử dụng chức năng này.");
                return;
            }
            if (detailUser.Rows.Count > 0)
            {
                int uid = int.Parse(Session["UserID"].ToString());
                try
                {
                    UserController controller = new UserController();
                    string hashedPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(Utilities.DefaultPassword, "sha1");

                    if (!controller.ResetPassword(uid, hashedPassword).Equals(string.Empty))
                    {
                        WebMsgBox.Show("Mật khẩu đã được reset.");
                    }
                }
                catch (Exception)
                {
                    Response.Redirect(Utilities.ErrorPagePath);
                    throw;
                }
            }
        }
    }
}
